package xyz.labmem.base.core.tool

import com.baomidou.mybatisplus.extension.activerecord.Model
import java.lang.reflect.Field
import kotlin.collections.ArrayList
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.superclasses


/**
 * 类工具
 * @Author lty
 * @Date 2022/1/12 14:14
 */

/**
 *获取类中所有的属性
 */
fun Class<*>.getClassAllField(): Array<Field> {
    var baseClazz = this
    val fieldsList = ArrayList<Field>().apply { addAll(baseClazz.declaredFields) }
    while (baseClazz.superclass != null && baseClazz.superclass != Model::class.java) {  // 遍历所有父类字节码对象
        baseClazz = baseClazz.superclass // 获得父类的字节码对象
        fieldsList.addAll(baseClazz.declaredFields)
    }
    return fieldsList.toTypedArray()
}

fun KClass<*>.getKClassAllField(): Array<KProperty<*>> {
    val fieldsList = ArrayList<KProperty<*>>()
    fieldsList.addAll(this.declaredMemberProperties)
    this.superclasses.forEach {
        if (it != Model::class) {
            fieldsList.addAll(it.declaredMemberProperties)
        }
    }
    return fieldsList.toTypedArray()
}


/**
 *获取类中所有的属性String
 */
fun Class<*>.getTarget(): Array<String> {
    return this::class.java.declaredFields.run {
        val list = ArrayList<String>()
        this.forEach { list.add(it.name) }
        list.toTypedArray()
    }
}